import datetime
import re
import sys

from aws_s3 import Operation_aws_s3
from base_class.inspect_logging import print_to_log
from config import *
from data_load_up import DataStep


def delete_day_file(month):
    print("删除:",month)
    s3 = Operation_aws_s3()
    path = f's3://{crm_bucket}/{crm_s3_path}p_date={month}/'
    pattern = r'^.*\d{4}-\d{2}-\d{2}.json$'
    file_list = s3.ls(path)
    if file_list:
        for file in file_list:
            if file:
                b = file.get('bucket')
                k = file.get("object")
                if re.match(pattern, k):
                    s3.empty_object(b, k)


if __name__ == '__main__':
    date_str = None
    if len(sys.argv) > 1:
        var = sys.argv[1]
        match1 = re.search(r"(\d{4}-\d{1,2}-\d{1,2})", var)
        match2 = re.search(r"(\d{4}-\d{1,2})", var)
        if match1 or match2:
            date_str = var
            print("输入参数", date_str)
        else:
            print_to_log("请输入正确参数")

    if date_str == None:
        today = datetime.datetime.today()
        # 昨天
        yesterday = (today - datetime.timedelta(days=1)).strftime('%Y-%m-%d')
        date_str = yesterday

    # 月分区字符串
    p = date_str[0:7]
    crmstep = DataStep(s3_path=crm_s3_path + f'p_date={p}/', url=crm_url, local_path=crm_local_path, buc=crm_bucket)
    Next = crmstep.download(date_str)
    if Next:
        crmstep.upload()

    # 如果是月度导入，先删除该月份数据
    if re.match(r'^\d{4}-\d{2}$', date_str):
        delete_day_file(month=date_str)
